package commonclass.dateandtime;




import org.junit.jupiter.api.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;

/**
 * @author Lenl
 * @version v1.0
 * @create 2021-06-04 1:15
 * @description JDK8中的新日期时间API
 *
 */
public class NewDateApi {
    /**
     * LocalDate、LocalTime、LocalDateTime
     *
     */
    @Test
    public  void test1(){
        //now() 获取当前的日期、时间、日期时间
        LocalDate localDate=LocalDate.now();
        LocalTime localTime=LocalTime.now();
        LocalDateTime localDateTime=LocalDateTime.now();

        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);

        //of() 设置指定的年月日时分秒，不用考虑偏移量
        LocalDateTime of = LocalDateTime.of(2020, 6, 4, 1, 26, 55);
        System.out.println(of);

        //getXxx()
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDateTime.getDayOfYear());
        System.out.println(localDateTime.getHour());

        //体现不可变性：withXxx() 设置相关属性
        LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(22);
        System.out.println("原日期："+localDateTime);
        System.out.println("现日期："+localDateTime1);

        //
        LocalDateTime localDateTime2 = localDateTime.plusDays(7);
        System.out.println(localDateTime2);
        localDateTime2=localDateTime.minusDays(7);
        System.out.println(localDateTime2);
    }

    /**
     *
     * Instant 瞬时
     *
     */
    @Test
    public void test2(){
        Instant instant=Instant.now();//这里算的是本初子午线的时间，要考虑时区问题
        System.out.println(instant);
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);

        //获取瞬时点对应的毫秒数
        long milli = instant.toEpochMilli();
        System.out.println(milli);

        //ofEpochMilli():通过给定毫秒数计算时间
        Instant instant1=Instant.ofEpochMilli(milli);
        System.out.println(instant1);
    }

    /**
     * DateTimeFormatter:格式化或解析日期、时间
     * 类似于SimpleDateFormatter
     *
     *
     */
    @Test
    public void test3(){
        //方式一：预定义的标准的格式。如 ISO_LOCAL_DATE_TIME等
        DateTimeFormatter formatter=DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化：日期-->字符串
        LocalDateTime localDateTime=LocalDateTime.now();
        String str1=formatter.format(localDateTime);
        System.out.println(localDateTime);
        System.out.println(str1);
        //解析
        TemporalAccessor parse = formatter.parse(str1);
        System.out.println(parse);

        //方式二：
        // 本地化相关的格式，如ofLocalizedDateTime()
                //FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于localDateTime
        DateTimeFormatter formatter1=DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
        //格式化
        String format = formatter1.format(localDateTime);
        System.out.println(format);
        //解析
        TemporalAccessor parse1 = formatter1.parse(format);
        System.out.println(parse1);
        //本地化相关的格式，如ofLocalizedDate()
                //FormatStyle.FULL/FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于localDate
        DateTimeFormatter formatter2=DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        String format1=formatter2.format(LocalDate.now());
        System.out.println(format1);
        TemporalAccessor parse2=formatter2.parse(format1);
        System.out.println(parse2);

        //重点 方式三：自定义的格式。如ofPattern("yyyy-MM-dd hh:MM:ss")
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:MM:ss");
        //格式化
        String str4=formatter3.format(localDateTime.now());
        System.out.println(str4);
        //解析
        TemporalAccessor parse3=formatter3.parse(str4);
        System.out.println(parse3);

    }

}
